// The form contains fields for many pagiantion optiosn so you can 
// quickly see the resuluts of the different options.
// This function creates an option object for the pagination function.
// This will be be unnecessary in your application where you just set
// the options once.
$(function() {
		var items_on_page = 10;
		var pages_step = 10;
		
		$("form#store_search").submit(function(){
			$.post("/stores/store_search", {
				name: $("#storeSearchSuggest").val(),
				category: $("#storeSearchCategory").val()
			}, function(text) {
				$("#demo_tbl").html(text);
			});

			$.ajax({
				type: "POST",
				url: "/stores/store_total",            
				data: {name: $("#storeSearchSuggest").val(),
				category: $("#storeSearchCategory").val(),
				type: "get"},
				success: function(total)
				{   
					$("#paging").jpaging({
						all_items_num: total,
						callback: get_data,
						items_on_page: items_on_page,
						pages_step: pages_step
					});
				}
			});  
			return false;
		});

		$("#storeSearchSuggest").focus().autocomplete("/stores/store_names", {
			width: 260,
			dataType:'json',
			selectFirst: false,
			minChars: 1,
			max:10,
			parse:function(data){
				var rows = new Array();
				for(var i = 0; i<data.length; i++){
					rows[rows.length] = {
					data:data[i],
					value:data[i],
					result:data[i]
					};
				}
				return rows;
			},
			formatItem:function(row,i,n){
				return row;
			}
			
		});

		function get_data(start_index, end_index){
			$.ajax({
				type: "POST",
				url: "/stores/store_paging",            
				data: {start_index: start_index,
				end_index: end_index,
				name: $("#storeSearchSuggest").val(),
				category: $("#storeSearchCategory").val(),
				type: "get"},
				success: function(html)
				{                  
					$("#demo_tbl").html(html);
				}
			});			
		}
		get_data(1, 10);
		$.ajax({
			type: "POST",
			url: "/stores/store_total",            
			data: {category: '0', type: "get"},
			success: function(total)
			{   
				$("#paging").jpaging({
					all_items_num: total,
					callback: get_data,
					items_on_page: items_on_page,
					pages_step: pages_step
				});
			}
		});  

		$.prettyDate.now = function() {
			return new Date();
		}
		$("a").prettyDate();
});
